<?php

/** 
 * @property CI_DB_query_builder $db
 */
class User extends Bg_Controller {
	const T_USER = 'hs_user';
	const T_RECHARGE = 'hs_recharge_order';
	const T_BUY = 'hs_order';
	const T_ORDER_VIEW = 'hs_order_view';
	const T_DEV = 'hs_dev';
	/**
	 */
	public function __construct() {
		parent::__construct ();
		$this->load->database();
		$this->load->model('Presrent_mdl','presrent');
	}
	
	/**
	 * 更新用户充值金额
	 */
	public function recharge(){
		// 获取充值成功且未添加到用户余额的订单列表
		$orders = $this->db->select('id,amount,userID')
					->from(self::T_RECHARGE)
					->where('tradeState','SUCCESS')
					->where('isAdded',0)
					->get()
					->result_array();
		
		echo count($orders),"\n";
		if( empty($orders) ){
			return 0;
		}
		foreach ( $orders as $order ){
			$orderID = (int)$order['id'];
			
			$presrent = 0;// 赠送金额
			$recharge = $order['amount'];//充值金额
//关闭赠送
// 			if( $recharge >= 10*100 ){//如果充值金额大于10元,赠送1元
// 				$presrent = 100;
// 				// 纪录到赠送纪录表
// 				$reason = "充值".($recharge/100)."元,赠送".($presrent/100).'元';
// 				$res = $this->presrent->record(0,$order['userID'],$presrent,$reason);
// 			}
			$amount = $presrent + $recharge;
			
			$sql = "UPDATE 
						`hs_user` AS u,`hs_recharge_order` AS o
					SET 
						u.`balance` = u.`balance` + $amount,
						u.`rechargeTotal` = u.`rechargeTotal` + $recharge,
						u.`presrent` = u.`presrent` + $presrent,
						o.`presrent` = o.`presrent` + $presrent,
						o.`isAdded` = 1
					WHERE 
							o.`id` = $orderID
						AND	u.`id` = o.`userID`
						AND	o.`isAdded` = 0";
			$this->db->query($sql);
			echo $this->db->affected_rows(),'--',preg_replace('/\s+/', ' ', $sql),"\n";
		}
	}
	/**
	 * 更新用户消费金额
	 */
	public function buy(){
		// 获取买水成功且未添加到用户累计消费的订单列表
		$orders = $this->db->select('id,amount,userID')
		->from(self::T_BUY)
		->where('isFinish',1)
		->where('isAdded',0)
		->get()
		->result_array();
		if( empty($orders) ){
			return 0;
		}
		
		foreach ( $orders as $order ){
			$orderID = (int)$order['id'];
			$sql = "UPDATE 
						`hs_user` AS u,`hs_order` AS o
					SET 
						u.`buyTotal` = u.`buyTotal` + o.`amount`,
						o.`isAdded` = 1
					WHERE 
							o.`id` = $orderID
						AND	u.`id` = o.`userID`
						AND	o.`isAdded` = 0";
			$this->db->query($sql);
			echo $this->db->affected_rows(),'--',preg_replace('/\s+/', ' ', $sql),"\n";
		}
	}
	
	/**
	 * 更新用户的学校ID
	 */
	public function school(){
		// 获取schoolID=0的用户列表
		$users = $this->db->select('id,schoolID')->from(self::T_USER)->where('schoolID',0)->get()->result_array();
		// 获取用户首次连接的设备ID
		$devIdList = array();
		foreach ($users as $k=>$user){
			$res = $this->db->select('devID')
							->from(self::T_BUY)
							->where('userID',$user['id'])
							->limit(1)
							->get()
							->row_array();
			if( ! empty($res['devID']) ){
				$users[$k]['devID'] = $res['devID'];
				$devIdList[$res['devID']] = $res['devID'];
			}else{
				unset($users[$k]);
			}
		}
		// 根据设备ID获取学校ID
		sort($devIdList);
		$devs = $this->db->select('id,schoolID')
							->from(self::T_DEV)
							->where_in('id',$devIdList)
							->get()
							->result_array();
		$devs = array_combine(array_column($devs, 'id'), $devs);
		// 更新用户的schoolID
		foreach ($users as $user){
			if (empty ( $devs [$user ['devID']] )) {
				continue;
			}
			$this->db->set('schoolID',$devs[$user['devID']]['schoolID'])
						->where('id',$user['id'])
						->update(self::T_USER);
		}
		$debug = get_defined_vars();
		$debug['queries'] = $this->db->queries;
		echo json_encode($debug,JSON_UNESCAPED_UNICODE);
		
	}
	
	
	public function temp(){
		// 获取充值成功且未添加到用户余额的订单列表
		$orders = $this->db->select('id,amount,userID')
		->from(self::T_RECHARGE)
		->where('tradeState','SUCCESS')
		->where('isAdded',0)
		->get()
		->result_array();
		
		echo count($orders),"\n";
		if( empty($orders) ){
			return 0;
		}
		foreach ( $orders as $order ){
			$orderID = (int)$order['id'];
			$sql = "
					UPDATE
						`hs_user` AS u,`hs_recharge_order` AS o
					SET
						u.`rechargeTotal` = u.`rechargeTotal` + o.`amount`,
						o.`isAdded` = 1
					WHERE
						o.`id` = $orderID
					AND	u.`id` = o.`userID`
					AND	o.`isAdded` = 0";
			$this->db->query($sql);
			echo $this->db->affected_rows(),'--',preg_replace('/\s+/', ' ', $sql),"\n";
		}
		
		$this->buy();
	}
	
	
}

